<?php

class FriendsController extends Zend_Controller_Action
{

    public function init()
    {
        try{
    		$session = Zend_Registry::get('session');
    		
    		if($session->user['Username'] != ""){ //ako ne e prazna sesijata znaci deka korisnikot e logiran.
    			
    		}        
        	else{        		
    			$this->_helper->redirector('index','login'); //vo sprotivno vrati go na stranata za logiranje.
    		} 
        
        }
        catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";  
        
        
    	}
    }

    public function indexAction()
    {
        $session = Zend_Registry::get('session');
    	$userID = $session->user['id'];   	//go zemame ID-to na korisnikot koj e momentalno logiran za da mu gi izlistame porakite koi se nameneti za nego	
    	
    	$friend_object = new Application_Model_DbTable_Friends();
    	$user_object = new Application_Model_DbTable_Users();
    	$basicInfo_object = new Application_Model_DbTable_Basicinfo();
    	
    	$this->view->basicinfo = $basicInfo_object;
    	$this->view->users = $user_object;
    	$this->view->friends = $friend_object->getFriends((int)$userID);
    }

    public function requestsAction()
    {
        $session = Zend_Registry::get('session');
    	$userID = $session->user['id'];   	//go zemame ID-to na korisnikot koj e momentalno logiran za da mu gi izlistame porakite koi se nameneti za nego	
    	
    	$friend_object = new Application_Model_DbTable_Friends();
    	$user_object = new Application_Model_DbTable_Users();
    	$basicInfo_object = new Application_Model_DbTable_Basicinfo();
    	
    	$this->view->basicinfo = $basicInfo_object;
    	$this->view->users = $user_object;
    	$this->view->friends = $friend_object->getFriends((int)$userID);
    	
    	$this->view->sentRequests = $friend_object->getSentPendingRequests($userID);		//vo sentRequests gi smetuvame site friend requests koi logiraniot korisnik gi ispratil do drugi korisnici
    	$this->view->receivedRequests = $friend_object->getReceivedPendingRequests($userID); // vo receivedRequests gi smetuvame friend requests koi se isprateni od drugi korisnici kon logiraniot korisnik
    	
    }

    public function acceptAction()
    {
        $senderID =  $this->getRequest()->getParam('sender');
        $receiverID =  $this->getRequest()->getParam('receiver');
        
        $friend_object = new Application_Model_DbTable_Friends();
        $friend_object->acceptFriendRequest($senderID, $receiverID);
        
        $this->_helper->redirector('requests');
    }

    public function rejectAction()
    {
        $id = $this->getRequest()->getParam('req_id');
        
        $friend_object = new Application_Model_DbTable_Friends();
        $friend_object->rejectRequest($id);
        
        $this->_helper->redirector('requests');
    }

    public function removeAction()
    {
        $user1ID =  $this->getRequest()->getParam('sender');
        $user2ID =  $this->getRequest()->getParam('receiver');
        
        $friend_object = new Application_Model_DbTable_Friends();
        $friend_object->removeFriend($user1ID, $user2ID);
        
        $this->_helper->redirector('index');
    }

    public function sendrequestAction()
    {
    	
        $senderID = $this->getRequest()->getParam('sender');
        $receiverID = $this->getRequest()->getParam('receiver');
        
        $friendOB = new Application_Model_DbTable_Friends();
        $check = $friendOB->checkRequest($senderID, $receiverID);
        
        if($check == 0){
        	$friendOB->sendFriendRequest($senderID, $receiverID);
        }
        
        $this->_helper->redirector('requests');
    }


}











